<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{&quot;hostname&quot;:&quot;www.nilstorm.com&quot;,&quot;root&quot;:&quot;&#x2F;&quot;,&quot;images&quot;:&quot;&#x2F;images&quot;,&quot;scheme&quot;:&quot;Gemini&quot;,&quot;version&quot;:&quot;8.5.0&quot;,&quot;exturl&quot;:false,&quot;sidebar&quot;:{&quot;position&quot;:&quot;left&quot;,&quot;display&quot;:&quot;post&quot;,&quot;padding&quot;:18,&quot;offset&quot;:12},&quot;copycode&quot;:false,&quot;bookmark&quot;:{&quot;enable&quot;:false,&quot;color&quot;:&quot;#222&quot;,&quot;save&quot;:&quot;auto&quot;},&quot;fancybox&quot;:false,&quot;mediumzoom&quot;:false,&quot;lazyload&quot;:false,&quot;pangu&quot;:false,&quot;comments&quot;:{&quot;style&quot;:&quot;tabs&quot;,&quot;active&quot;:null,&quot;storage&quot;:true,&quot;lazyload&quot;:false,&quot;nav&quot;:null},&quot;motion&quot;:{&quot;enable&quot;:true,&quot;async&quot;:false,&quot;transition&quot;:{&quot;post_block&quot;:&quot;fadeIn&quot;,&quot;post_header&quot;:&quot;fadeInDown&quot;,&quot;post_body&quot;:&quot;fadeInDown&quot;,&quot;coll_header&quot;:&quot;fadeInLeft&quot;,&quot;sidebar&quot;:&quot;fadeInUp&quot;}},&quot;prism&quot;:false,&quot;i18n&quot;:{&quot;placeholder&quot;:&quot;Searching...&quot;,&quot;empty&quot;:&quot;We didn&#39;t find any results for the search: ${query}&quot;,&quot;hits_time&quot;:&quot;${hits} results found in ${time} ms&quot;,&quot;hits&quot;:&quot;${hits} results found&quot;}}</script><script src="/js/config.js"></script>
<meta name="description" content="前言因为最近事情太多，导致原本每月至少一篇的计划整整延迟了一个季度，所幸现在大多事情都比较明确和稳定了，因此接下来会保持基本每月至少一篇的进度，同时渲染库也会正式开始进行，感兴趣的提前Star关注一下～ GPUImage-X ：以游戏引擎的效果和特性，支持图片&#x2F;视频等场景下的跨平台3D渲染库">
<meta property="og:type" content="article">
<meta property="og:title" content="跨平台渲染引擎之路：bgfx再分析与启程">
<meta property="og:url" content="http://www.nilstorm.com/2020/8f38e399.html">
<meta property="og:site_name" content="Lam&#39;s Blog">
<meta property="og:description" content="前言因为最近事情太多，导致原本每月至少一篇的计划整整延迟了一个季度，所幸现在大多事情都比较明确和稳定了，因此接下来会保持基本每月至少一篇的进度，同时渲染库也会正式开始进行，感兴趣的提前Star关注一下～ GPUImage-X ：以游戏引擎的效果和特性，支持图片&#x2F;视频等场景下的跨平台3D渲染库">
<meta property="og:locale">
<meta property="og:image" content="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg">
<meta property="article:published_time" content="2020-05-02T05:54:36.000Z">
<meta property="article:modified_time" content="2021-06-06T08:08:18.176Z">
<meta property="article:author" content="LinBinghe">
<meta property="article:tag" content="跨平台">
<meta property="article:tag" content="渲染引擎">
<meta property="article:tag" content="bgfx">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg">


<link rel="canonical" href="http://www.nilstorm.com/2020/8f38e399.html">



<script class="next-config" data-name="page" type="application/json">{&quot;sidebar&quot;:&quot;&quot;,&quot;isHome&quot;:false,&quot;isPost&quot;:true,&quot;lang&quot;:&quot;zh-Hans&quot;,&quot;comments&quot;:true,&quot;permalink&quot;:&quot;http:&#x2F;&#x2F;www.nilstorm.com&#x2F;2020&#x2F;8f38e399.html&quot;,&quot;path&quot;:&quot;2020&#x2F;8f38e399.html&quot;,&quot;title&quot;:&quot;跨平台渲染引擎之路：bgfx再分析与启程&quot;}</script>

<script class="next-config" data-name="calendar" type="application/json">&quot;&quot;</script>
<title>跨平台渲染引擎之路：bgfx再分析与启程 | Lam's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<link rel="alternate" href="/atom.xml" title="Lam's Blog" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lam's Blog</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">Knowledge as Action</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%8D%E4%B8%80%E6%A0%B7%E7%9A%84%E6%B8%B2%E6%9F%93%E5%BA%93"><span class="nav-number">2.</span> <span class="nav-text">不一样的渲染库</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#bgfx%E5%AE%9E%E6%88%98"><span class="nav-number">3.</span> <span class="nav-text">bgfx实战</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9B%E5%BB%BADemo"><span class="nav-number">3.1.</span> <span class="nav-text">创建Demo</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#shaderc"><span class="nav-number">3.2.</span> <span class="nav-text">shaderc</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B8%B2%E6%9F%93%E4%B8%80%E5%BC%A0%E7%BA%B9%E7%90%86"><span class="nav-number">3.3.</span> <span class="nav-text">渲染一张纹理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%A0%E5%8A%A0%E6%BB%A4%E9%95%9C%E9%93%BE"><span class="nav-number">3.4.</span> <span class="nav-text">叠加滤镜链</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%9A%E7%BA%B9%E7%90%86%E6%B7%B7%E5%90%88"><span class="nav-number">3.5.</span> <span class="nav-text">多纹理混合</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%A2%98%E5%A4%96%E8%AF%9D"><span class="nav-number">4.</span> <span class="nav-text">题外话</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-number">5.</span> <span class="nav-text">总结</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LinBinghe"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">LinBinghe</p>
  <div class="site-description" itemprop="description">Knowledge as Action</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">39</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">60</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LinBinghe" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LinBinghe" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/shawn_lam" title="Zhihu → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;shawn_lam" rel="noopener" target="_blank"><i class="fab fa-zhihu fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:linbinghe@gmail.com" title="E-Mail → mailto:linbinghe@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="http://www.nilstorm.com/2020/8f38e399.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="LinBinghe">
      <meta itemprop="description" content="Knowledge as Action">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lam's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          跨平台渲染引擎之路：bgfx再分析与启程
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2020-05-02 13:54:36" itemprop="dateCreated datePublished" datetime="2020-05-02T13:54:36+08:00">2020-05-02</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-06-06 16:08:18" itemprop="dateModified" datetime="2021-06-06T16:08:18+08:00">2021-06-06</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%B8%B2%E6%9F%93%E5%BC%95%E6%93%8E/" itemprop="url" rel="index"><span itemprop="name">渲染引擎</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>因为最近事情太多，导致原本每月至少一篇的计划整整延迟了一个季度，所幸现在大多事情都比较明确和稳定了，因此接下来会保持基本每月至少一篇的进度，同时渲染库也会正式开始进行，感兴趣的提前<code>Star</code>关注一下～</p>
<p><a target="_blank" rel="noopener" href="https://github.com/LinBinghe/GPUImage-X">GPUImage-X</a> ：以游戏引擎的效果和特性，支持图片/视频等场景下的跨平台3D渲染库<br><span id="more"></span></p>
<h1 id="不一样的渲染库"><a href="#不一样的渲染库" class="headerlink" title="不一样的渲染库"></a>不一样的渲染库</h1><p>在之前研究阶段我们看到的大多都是以游戏为需求出发的游戏引擎，这类引擎一方面代码量庞大，架构复杂，包罗万象，在现阶段直接以游戏引擎或者类似bgfx的渲染引擎为目标从0开始撸一套明显会有些力不从心，而且这个重复造轮子的程度就有点高了，因此是否可以有一种方式能让我们既可以从中学习到渲染引擎以及之上的各个效果系统的实现方式，同时又能去适应其他不同的场景，增加我们的成就感呢？</p>
<p>现在市场上各类社交、工具中都有视频和图片的场景，并且用户都喜欢在上面添加各类特效，甚至也已经看到了各种3D的玩法，但是在我尝试使用Urho3D等游戏引擎来适配这类场景的时候，发现使用难度较大，因为这些游戏引擎的初衷并不是为了这样的场景而设计的，引擎内的很多系统也都是这类场景所不需要的，导致编译出来的库也较大，同时学习成本也是难以想象的。</p>
<p>而目前开源的库中添加图像效果的最主流的就是GPUImage这个库，但是这个库基本只能添加图像处理方面的滤镜效果，有Transform的滤镜但是并不能符合3D的需求，同时粒子效果、光照等目前也是不支持的。</p>
<p>因此在接下来这段时间里，我会以bgfx来提供底层渲染API的封装和多平台渲染后端的支持，在之上搭建支持这类场景（图片/视频叠加特效的同时还需要进行一些3D渲染、粒子等更丰富的内容）的跨平台渲染库，架构设计等内容正在准备中，因为这个库理论上是在GPUImage的场景支持基础上进行扩展，让能力变得更加强大，同时将其变为跨平台的一个库，因此命名为 <a target="_blank" rel="noopener" href="https://github.com/LinBinghe/GPUImage-X">GPUImage-X</a> ，预计本月内会完成整体框架的搭建并先可渲染滤镜链，届时也会针对架构设计、流程设计、规划等方面发表一篇文章，感兴趣的可以先<code>Star</code>一下～</p>
<h1 id="bgfx实战"><a href="#bgfx实战" class="headerlink" title="bgfx实战"></a>bgfx实战</h1><p>既然我们确定在bgfx之上搭建我们的渲染库，那么就需要先在bgfx上小试牛刀一下，至少对bgfx我们第一阶段比较关心的一些用法比较清楚，而第一阶段的目标是完成整体框架的落地以及实现其中的滤镜链系统，因此我们先以以下几个点作为切入，在bgfx之上写一个简单的Demo来确定使用方式：</p>
<ul>
<li>渲染一张纹理</li>
<li>在纹理上叠加滤镜链</li>
<li>多纹理混合</li>
</ul>
<h2 id="创建Demo"><a href="#创建Demo" class="headerlink" title="创建Demo"></a>创建Demo</h2><p>为了避免修改bgfx自身所带的Demo，所以我们自己创建一个调试用的Demo。</p>
<p>首先在<code>bgfx/examples</code>目录下创建一个名为<code>41-filter</code>的目录，在该目录内放置以下文件：</p>
<ul>
<li>filter.cpp：Demo代码</li>
<li>fs_filter_bw.sc：bgfx着色器规则下的将纹理渲染为灰度图的片段着色器</li>
<li>fs_filter_normal.sc：bgfx着色器规则下的将纹理简单渲染出来的片段着色器</li>
<li>makefile：配置编译规则，可在编译的时候直接将我们的着色器编译成bgfx支持的.bin格式文件放置到<code>runtime/shaders</code>的各个着色语言目录下</li>
<li>varying.def.sc：着色器的参数声明</li>
<li>vs_filter_normal.sc：bgfx着色器规则下顶点着色器</li>
</ul>
<p>具体代码内容见<a target="_blank" rel="noopener" href="https://github.com/LinBinghe/bgfx">fork的bgfx</a>，后续有些效果为了快速验证也会先在这个仓库上先进行Demo验证，然后实现到<a target="_blank" rel="noopener" href="https://github.com/LinBinghe/GPUImage-X">GPUImage-X</a>上，而语法方面大家如果熟悉glsl，通过官方说明和配合其他Demo里的着色器摸索一下应该问题不大。</p>
<p>在添加上述文件后，还需要修改以下两个文件：</p>
<ul>
<li>examples/makefile：在<code>@make -s --no-print-directory rebuild -C 40-svt</code>下面加一行：<code>@make -s --no-print-directory rebuild -C 41-filter</code></li>
<li>scripts/genia.lua：在<code>, &quot;40-svt&quot;</code>下面加一行：<code>, &quot;41-filter&quot;</code></li>
</ul>
<p>上面操作做完之后，命令行下回到bgfx的根目录，按照官方的指令编译出工程（笔者是在Mac OS的环境下）：</p>
<ul>
<li>../bx/tools/bin/darwin/genie —with-combined-examples —xcode=osx xcode9</li>
<li>open .build/projects/xcode9-osx/bgfx.xcworkspace</li>
</ul>
<p>运行<code>examples-debug</code>，即可在Demo列表的最下面看到我们的<code>41-filter</code>Demo了。</p>
<p><em>笔者自己手动在<code>runtimes/iamges</code>下放了一张<code>fengjing.jpg</code>的图片作为纹理显示，大家可以随便放一个图片进去，命名一致即可，如果不一致就需要修改<code>filter.cpp</code>里的记载的纹理名</em></p>
<h2 id="shaderc"><a href="#shaderc" class="headerlink" title="shaderc"></a>shaderc</h2><p>首先通过上面的操作我们会发现，在编译结束后脚本并不会自动将这些着色器编译成各类着色器语言的.bin文件并放置到<code>runtime/shaders</code>下（暂未找到在哪里配置，后续嫌麻烦找到了再补上～），而<code>runtime</code>这个目录里正式存储着所有在运行时会用到的资源（包括上面的图片），因此除了在一开始我们就配置好编译出来，在过程中我们也可以直接往<code>runtime</code>下面扔资源，Demo也是可以找得到的。</p>
<p>那么这时候我们就可以用到bgfx提供给我们的<code>shaderc</code>工具了。</p>
<p>首先在clone下来的bgfx仓库中是没有<code>shaderc</code>的可执行文件的，因此需要先进行编译，在bgfx的根目录下运行<code>make tools</code>，然后等你打把游戏回来之后，就会发现<code>tools/bin/darwin</code>下面有了一些执行文件。</p>
<p>结合下我们上面Demo里的着色器相关的几个文件，可以发现在bgfx里面着色器的编译一般包含以下三个文件：</p>
<ul>
<li>顶点着色器</li>
<li>片段着色器</li>
<li>着色器参数说明</li>
</ul>
<p>而编译的时候顶点着色器和片段着色器是分开编译，但是必须都声明其着色器说明文件路径，以<code>41-filter</code>为例，首先我们先命令行cd到该目录下，然后以下两条命令即是表示编译Mac OS下的Metal着色器：</p>
<ul>
<li>../../tools/bin/darwin/shaderc -f vs_filter_normal.sc -o vs_filter_normal.bin —depends -i ../../src —varyingdef varying.def.sc —platform osx -p metal —type vertex</li>
<li>../../tools/bin/darwin/shaderc -f fs_filter_bw.sc -o fs_filter_bw.bin —depends -i ../../src —varyingdef varying.def.sc —platform osx -p metal —type fragment</li>
</ul>
<p>第一条是顶点着色器，第二条是片段着色器，生成后把.bin文件放置到<code>runtime/shaders/metal</code>下即可，具体参数的说明，直接命令行调用<code>shaderc</code>也会出现帮助信息</p>
<h2 id="渲染一张纹理"><a href="#渲染一张纹理" class="headerlink" title="渲染一张纹理"></a>渲染一张纹理</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * Copyright 2011-2019 Branimir Karadzic. All rights reserved.</span></span><br><span class="line"><span class="comment"> * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bx/uint32_t.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;common.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;bgfx_utils.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;imgui/imgui.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">namespace</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">//顶点以及纹理坐标对象</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">PosTexVertex</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">float</span> m_x;</span><br><span class="line">    <span class="keyword">float</span> m_y;</span><br><span class="line">    <span class="keyword">float</span> m_z;</span><br><span class="line">    <span class="keyword">float</span> t_x;</span><br><span class="line">    <span class="keyword">float</span> t_y;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">()</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        ms_decl</span><br><span class="line">        .<span class="built_in">begin</span>()</span><br><span class="line">        .<span class="built_in">add</span>(bgfx::Attrib::Position, <span class="number">3</span>, bgfx::AttribType::Float)</span><br><span class="line">        .<span class="built_in">add</span>(bgfx::Attrib::TexCoord0,   <span class="number">2</span>, bgfx::AttribType::Float)</span><br><span class="line">        .<span class="built_in">end</span>();</span><br><span class="line">    &#125;;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">static</span> bgfx::VertexDecl ms_decl;</span><br><span class="line">&#125;;</span><br><span class="line">    </span><br><span class="line">bgfx::VertexDecl PosTexVertex::ms_decl;</span><br><span class="line"><span class="comment">//顶点坐标数据</span></span><br><span class="line"><span class="keyword">static</span> PosTexVertex s_vertices[] =</span><br><span class="line">&#123;</span><br><span class="line">    &#123;<span class="number">-1.0f</span>,  <span class="number">1.0f</span>,  <span class="number">0.0f</span>, <span class="number">0.0f</span>, <span class="number">0.0f</span> &#125;,</span><br><span class="line">    &#123; <span class="number">1.0f</span>,  <span class="number">1.0f</span>,  <span class="number">0.0f</span>, <span class="number">1.0f</span>, <span class="number">0.0f</span> &#125;,</span><br><span class="line">    &#123;<span class="number">-1.0f</span>, <span class="number">-1.0f</span>,  <span class="number">0.0f</span>, <span class="number">0.0f</span>, <span class="number">1.0f</span> &#125;,</span><br><span class="line">    &#123; <span class="number">1.0f</span>, <span class="number">-1.0f</span>,  <span class="number">0.0f</span>, <span class="number">1.0f</span>, <span class="number">1.0f</span> &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//顶点顺序</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">uint16_t</span> s_triList[] =</span><br><span class="line">&#123;</span><br><span class="line">    <span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="comment">// 0</span></span><br><span class="line">    <span class="number">1</span>, <span class="number">3</span>, <span class="number">2</span>,</span><br><span class="line">&#125;;</span><br><span class="line">    </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ExampleFilter</span> :</span> <span class="keyword">public</span> entry::AppI</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">	<span class="built_in">ExampleFilter</span>(<span class="keyword">const</span> <span class="keyword">char</span>* _name, <span class="keyword">const</span> <span class="keyword">char</span>* _description)</span><br><span class="line">		: entry::<span class="built_in">AppI</span>(_name, _description)</span><br><span class="line">	&#123;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">init</span><span class="params">(<span class="keyword">int32_t</span> _argc, <span class="keyword">const</span> <span class="keyword">char</span>* <span class="keyword">const</span>* _argv, <span class="keyword">uint32_t</span> _width, <span class="keyword">uint32_t</span> _height)</span> <span class="keyword">override</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="function">Args <span class="title">args</span><span class="params">(_argc, _argv)</span></span>;</span><br><span class="line"></span><br><span class="line">		m_width  = _width;</span><br><span class="line">		m_height = _height;</span><br><span class="line">		m_debug  = BGFX_DEBUG_TEXT;</span><br><span class="line">		m_reset  = BGFX_RESET_VSYNC;</span><br><span class="line"></span><br><span class="line">		bgfx::Init init;</span><br><span class="line">		init.type     = args.m_type;</span><br><span class="line">		init.vendorId = args.m_pciId;</span><br><span class="line">		init.resolution.width  = m_width;</span><br><span class="line">		init.resolution.height = m_height;</span><br><span class="line">		init.resolution.reset  = m_reset;</span><br><span class="line">		bgfx::<span class="built_in">init</span>(init);</span><br><span class="line"></span><br><span class="line">		<span class="comment">// Enable debug text.</span></span><br><span class="line">		bgfx::<span class="built_in">setDebug</span>(m_debug);</span><br><span class="line"></span><br><span class="line">		<span class="comment">// Set view 0 clear state.</span></span><br><span class="line">		bgfx::<span class="built_in">setViewClear</span>(<span class="number">0</span></span><br><span class="line">			, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH</span><br><span class="line">			, <span class="number">0x303030ff</span></span><br><span class="line">			, <span class="number">1.0f</span></span><br><span class="line">			, <span class="number">0</span></span><br><span class="line">			);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Create vertex stream declaration.</span></span><br><span class="line">        PosTexVertex::<span class="built_in">init</span>();</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// Create static vertex buffer.</span></span><br><span class="line">        m_vbh = bgfx::<span class="built_in">createVertexBuffer</span>(</span><br><span class="line">                                         <span class="comment">// Static data can be passed with bgfx::makeRef</span></span><br><span class="line">                                         bgfx::<span class="built_in">makeRef</span>(s_vertices, <span class="built_in"><span class="keyword">sizeof</span></span>(s_vertices) )</span><br><span class="line">                                         , PosTexVertex::ms_decl</span><br><span class="line">                                         );</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// Create static index buffer for triangle list rendering.</span></span><br><span class="line">        m_ibh = bgfx::<span class="built_in">createIndexBuffer</span>(</span><br><span class="line">                                           <span class="comment">// Static data can be passed with bgfx::makeRef</span></span><br><span class="line">                                           bgfx::<span class="built_in">makeRef</span>(s_triList, <span class="built_in"><span class="keyword">sizeof</span></span>(s_triList) )</span><br><span class="line">                                           );</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// Create program from shaders.</span></span><br><span class="line">        m_program = <span class="built_in">loadProgram</span>(<span class="string">&quot;vs_filter_normal&quot;</span>, <span class="string">&quot;fs_filter_normal&quot;</span>);</span><br><span class="line">        m_texture = <span class="built_in">loadTexture</span>(<span class="string">&quot;images/fengjing.jpg&quot;</span>);</span><br><span class="line">        u_Texture = bgfx::<span class="built_in">createUniform</span>(<span class="string">&quot;s_texColor&quot;</span>, bgfx::UniformType::Sampler);</span><br><span class="line">        </span><br><span class="line">		<span class="built_in">imguiCreate</span>();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">virtual</span> <span class="keyword">int</span> <span class="title">shutdown</span><span class="params">()</span> <span class="keyword">override</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="built_in">imguiDestroy</span>();</span><br><span class="line">		</span><br><span class="line">		bgfx::<span class="built_in">destroy</span>(m_ibh);</span><br><span class="line">        bgfx::<span class="built_in">destroy</span>(m_vbh);</span><br><span class="line">        bgfx::<span class="built_in">destroy</span>(m_program);</span><br><span class="line">        bgfx::<span class="built_in">destroy</span>(m_texture);</span><br><span class="line">        bgfx::<span class="built_in">destroy</span>(u_Texture);</span><br><span class="line">        </span><br><span class="line">		<span class="comment">// Shutdown bgfx.</span></span><br><span class="line">		bgfx::<span class="built_in">shutdown</span>();</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">bool</span> <span class="title">update</span><span class="params">()</span> <span class="keyword">override</span></span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		<span class="keyword">if</span> (!entry::<span class="built_in">processEvents</span>(m_width, m_height, m_debug, m_reset, &amp;m_mouseState) )</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">imguiBeginFrame</span>(m_mouseState.m_mx</span><br><span class="line">				,  m_mouseState.m_my</span><br><span class="line">				, (m_mouseState.m_buttons[entry::MouseButton::Left  ] ? IMGUI_MBUT_LEFT   : <span class="number">0</span>)</span><br><span class="line">				| (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT  : <span class="number">0</span>)</span><br><span class="line">				| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : <span class="number">0</span>)</span><br><span class="line">				,  m_mouseState.m_mz</span><br><span class="line">				, <span class="built_in">uint16_t</span>(m_width)</span><br><span class="line">				, <span class="built_in">uint16_t</span>(m_height)</span><br><span class="line">				);</span><br><span class="line"></span><br><span class="line">			<span class="built_in">showExampleDialog</span>(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">			<span class="built_in">imguiEndFrame</span>();</span><br><span class="line"></span><br><span class="line">			<span class="comment">// Set view 0 default viewport.</span></span><br><span class="line">			bgfx::<span class="built_in">setViewRect</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="built_in">uint16_t</span>(m_width), <span class="built_in">uint16_t</span>(m_height) );</span><br><span class="line"></span><br><span class="line">			<span class="comment">// This dummy draw call is here to make sure that view 0 is cleared</span></span><br><span class="line">			<span class="comment">// if no other draw calls are submitted to view 0.</span></span><br><span class="line">			bgfx::<span class="built_in">touch</span>(<span class="number">0</span>);</span><br><span class="line">            </span><br><span class="line">            <span class="keyword">uint64_t</span> state = <span class="number">0</span></span><br><span class="line">            | BGFX_STATE_WRITE_R</span><br><span class="line">            | BGFX_STATE_WRITE_G</span><br><span class="line">            | BGFX_STATE_WRITE_B</span><br><span class="line">            | BGFX_STATE_WRITE_A</span><br><span class="line">            | <span class="built_in">UINT64_C</span>(<span class="number">0</span>)</span><br><span class="line">            ;</span><br><span class="line">            </span><br><span class="line">            bgfx::<span class="built_in">setViewRect</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="built_in">uint16_t</span>(m_width), <span class="built_in">uint16_t</span>(m_height)); <span class="comment">//设置View0的视口</span></span><br><span class="line">            bgfx::<span class="built_in">setVertexBuffer</span>(<span class="number">0</span>, m_vbh);<span class="comment">//设置stream0的vertexBuffer，注意第一个参数不是viewId</span></span><br><span class="line">            bgfx::<span class="built_in">setIndexBuffer</span>(m_ibh);<span class="comment">//设置顶点索引buffer数据</span></span><br><span class="line">            bgfx::<span class="built_in">setState</span>(state);<span class="comment">//设置控制绘制信息的标志位</span></span><br><span class="line">            bgfx::<span class="built_in">setTexture</span>(<span class="number">0</span>, u_Texture, m_texture);<span class="comment">//设置对应的u_Texture这个着色器参数的纹理资源</span></span><br><span class="line">            bgfx::<span class="built_in">submit</span>(<span class="number">0</span>, m_program);<span class="comment">//提交绘制单张纹理的Program</span></span><br><span class="line"></span><br><span class="line">			<span class="comment">// Advance to next frame. Rendering thread will be kicked to</span></span><br><span class="line">			<span class="comment">// process submitted rendering primitives.</span></span><br><span class="line">			bgfx::<span class="built_in">frame</span>();</span><br><span class="line"></span><br><span class="line">			<span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	entry::MouseState m_mouseState;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">uint32_t</span> m_width;</span><br><span class="line">	<span class="keyword">uint32_t</span> m_height;</span><br><span class="line">	<span class="keyword">uint32_t</span> m_debug;</span><br><span class="line">	<span class="keyword">uint32_t</span> m_reset;</span><br><span class="line">    </span><br><span class="line">    bgfx::VertexBufferHandle m_vbh;</span><br><span class="line">    bgfx::IndexBufferHandle m_ibh;</span><br><span class="line">    bgfx::TextureHandle m_texture;</span><br><span class="line">    bgfx::UniformHandle u_Texture;</span><br><span class="line">    bgfx::ProgramHandle m_program;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">&#125; <span class="comment">// namespace</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">ENTRY_IMPLEMENT_MAIN</span>(ExampleFilter, <span class="string">&quot;41-filter&quot;</span>, <span class="string">&quot;Initialization and show filter effects.&quot;</span>);</span><br></pre></td></tr></table></figure>
<p>必要的地方都有加上了注释，首先我们用一个顶点数据对象来存放我们的顶点数据，定义了顶点坐标和索引，在界面初始化时加载了Program和纹理等资源，在update刷新的时候我们通过bgfx对视口、VBO等内容进行了设置，最终提交了要绘制的Program，这时候bgfx会绘制到backbuffer上，我们调用bgfx::frame()将backbuffer内容显示到屏幕上，主体流程与我们平常自己使用OpenGL还是比较接近的。</p>
<p>最后不要忘了在shutdown中释放资源，如果忘记释放资源，bgfx的日志会提醒存在泄漏，这点我觉得是非常不错的。</p>
<h2 id="叠加滤镜链"><a href="#叠加滤镜链" class="headerlink" title="叠加滤镜链"></a>叠加滤镜链</h2><p>首先我们再声明一个ProgramHandle，用于加载灰度滤镜：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">bgfx::ProgramHandle m_chain_program;</span><br><span class="line">m_chain_program = <span class="built_in">loadProgram</span>(<span class="string">&quot;vs_filter_normal&quot;</span>, <span class="string">&quot;fs_filter_bw&quot;</span>);</span><br></pre></td></tr></table></figure>
<p>然后创建一个FrameBuffer来缓存第一个滤镜的结果：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">bgfx::FrameBufferHandle m_frameBuffer;</span><br><span class="line">m_frameBuffer = bgfx::<span class="built_in">createFrameBuffer</span>(m_width, m_height, bgfx::TextureFormat::BGRA8);</span><br></pre></td></tr></table></figure>
<p>最后修改我们update中绘制的代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// This dummy draw call is here to make sure that view 0 is cleared</span></span><br><span class="line"><span class="comment">// if no other draw calls are submitted to view 0.</span></span><br><span class="line">bgfx::<span class="built_in">touch</span>(<span class="number">0</span>);</span><br><span class="line">bgfx::<span class="built_in">setViewRect</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="built_in">uint16_t</span>(m_width), <span class="built_in">uint16_t</span>(m_height) );<span class="comment">//设置View0的视口</span></span><br><span class="line">bgfx::<span class="built_in">setViewFrameBuffer</span>(<span class="number">0</span>, m_frameBuffer);<span class="comment">//设置绘制到FrameBuffer上</span></span><br><span class="line"></span><br><span class="line">bgfx::<span class="built_in">touch</span>(<span class="number">1</span>);</span><br><span class="line">bgfx::<span class="built_in">setViewRect</span>(<span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="built_in">uint16_t</span>(m_width), <span class="built_in">uint16_t</span>(m_height) );</span><br><span class="line">bgfx::<span class="built_in">setViewFrameBuffer</span>(<span class="number">1</span>, BGFX_INVALID_HANDLE);</span><br><span class="line"></span><br><span class="line"><span class="comment">//声明和设置我们的绘制顺序</span></span><br><span class="line">bgfx::ViewId order[] =</span><br><span class="line">&#123;</span><br><span class="line">    <span class="number">0</span>,<span class="number">1</span></span><br><span class="line">&#125;;</span><br><span class="line">bgfx::<span class="built_in">setViewOrder</span>(<span class="number">0</span>, <span class="built_in">BX_COUNTOF</span>(order), order);</span><br><span class="line"></span><br><span class="line">bgfx::<span class="built_in">setVertexBuffer</span>(<span class="number">0</span>, m_vbh);<span class="comment">//设置stream0的vertexBuffer，注意第一个参数不是viewId</span></span><br><span class="line">bgfx::<span class="built_in">setIndexBuffer</span>(m_ibh);<span class="comment">//设置顶点索引buffer数据</span></span><br><span class="line">bgfx::<span class="built_in">setState</span>(state);<span class="comment">//设置控制绘制信息的标志位</span></span><br><span class="line">bgfx::<span class="built_in">setTexture</span>(<span class="number">0</span>, u_Texture, m_texture);<span class="comment">//设置对应的u_Texture这个着色器参数的纹理资源</span></span><br><span class="line">bgfx::<span class="built_in">submit</span>(<span class="number">0</span>, m_program);<span class="comment">//提交绘制单张纹理的Program</span></span><br><span class="line"></span><br><span class="line">bgfx::<span class="built_in">setVertexBuffer</span>(<span class="number">0</span>, m_vbh);</span><br><span class="line">bgfx::<span class="built_in">setIndexBuffer</span>(m_ibh);</span><br><span class="line">bgfx::<span class="built_in">setState</span>(state);</span><br><span class="line">bgfx::<span class="built_in">setTexture</span>(<span class="number">0</span>, u_Texture, bgfx::<span class="built_in">getTexture</span>(m_frameBuffer));<span class="comment">//设置对应的u_Texture这个着色器参数的纹理资源，即上一次的绘制结果</span></span><br><span class="line">bgfx::<span class="built_in">submit</span>(<span class="number">1</span>, m_chain_program);</span><br></pre></td></tr></table></figure>
<p>基本上和我们OpenGL绘制滤镜链也是相近的，需要注意的点是bgfx再submit之后之前的数据都会清除，所以需要重新设置，同时需要设置一下渲染的view的顺序。</p>
<h2 id="多纹理混合"><a href="#多纹理混合" class="headerlink" title="多纹理混合"></a>多纹理混合</h2><p>多纹理混合这块暂时没有写demo，因为这块比较简单，通过bgfx::setState的时候通过<code>BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_SRC_COLOR)</code>这样的方式来制定混合模式即可，当然也可以通过自己写着色器来进行混合。</p>
<h1 id="题外话"><a href="#题外话" class="headerlink" title="题外话"></a>题外话</h1><p>bgfx这个库在代码实现上有些地方的确比较难看懂，而且注释、文档也比较简洁，但是整个库的设计思路还是很不错的，通过这个库可以学到很多东西，比如在renderer_gl中搜索<code>CASE_IMPLEMENT_UNIFORM</code>，在找找上下文，会发现bgfx通过<code>glGetProgramiv(m_id, GL_ACTIVE_ATTRIBUTES, &amp;activeAttribs )</code>之类的接口来获取Program中所有的参数，然后将参数长度、类型等信息存储到自定义的数据结构中，最后在通过宏定义和组装的方式，简洁地进行了参数设置。</p>
<p>并且bgfx自身也提供了许多Demo，通过这些Demo基本可以学到大多数渲染效果的实现方式，这个库的作者还有一个聊天室，回复的速度基本是当天之内，所以还是非常不错和值得学习的一个库的。</p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>本篇主要介绍了一下接下来要开工的<a target="_blank" rel="noopener" href="https://github.com/LinBinghe/GPUImage-X">GPUImage-X</a>的适用场景，即将3D、粒子、动画等效果融合到特效中，并主打图片/视频这类场景中，同时底层会直接使用bgfx来作为渲染API封装层，预计在本月内会完成初版的落地，因此下一篇文章将会介绍该库的框架、模块、内部流程以及将来更细致的规划，还会包括在实现过程中踩过的坑和技术点，感兴趣的可以先关注一下，这次保证不跳票～</p>
<hr>
<p><strong>版权声明</strong></p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1gee6wkfo69j30e9026gm4.jpg" alt="Creative Commons BY-NC-ND 4.0 International License"></p>
<p><a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog</a> by <a target="_blank" rel="noopener" href="http://linbinghe.me/">Binghe Lin</a> is licensed under a <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons BY-NC-ND 4.0 International License</a>.<br>由<a target="_blank" rel="noopener" href="http://linbinghe.me/">林炳河</a>创作并维护的<a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog</a>采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">创作共用保留署名-非商业-禁止演绎4.0国际许可证</a>。</p>
<p>本文首发于<a target="_blank" rel="noopener" href="http://linbinghe.me/">Lam’s Blog - Knowledeg as Action</a>，版权所有，侵权必究。</p>
<p>本文永久链接：<a target="_blank" rel="noopener" href="http://codinglife.me/2020/8f38e399.html">http://codinglife.me/2020/8f38e399.html</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E8%B7%A8%E5%B9%B3%E5%8F%B0/" rel="tag"># 跨平台</a>
              <a href="/tags/%E6%B8%B2%E6%9F%93%E5%BC%95%E6%93%8E/" rel="tag"># 渲染引擎</a>
              <a href="/tags/bgfx/" rel="tag"># bgfx</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2020/9382d2c0.html" rel="prev" title="跨平台渲染引擎之路：Urho3D分析与数据驱动">
                  <i class="fa fa-chevron-left"></i> 跨平台渲染引擎之路：Urho3D分析与数据驱动
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2020/ba69df2a.html" rel="next" title="跨平台渲染引擎之路：框架与核心模块">
                  跨平台渲染引擎之路：框架与核心模块 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LinBinghe</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  






  





</body>
</html>
